package com.koron.tool.controller;

import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.annocation.CasUser;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.tool.bean.ToolInspectionBean;
import com.koron.tool.bean.ToolInspectionQuery;
import com.koron.tool.bean.ToolQuery;
import com.koron.tool.service.ToolInspectionManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @Author: liaodf
 * @Date: 2021/10/25 17:29
 */

@Slf4j
@Api(tags = "工器具检验管理")
@RestController
@RequestMapping("/inspection")
public class ToolInspectionManagerController implements WorkflowController {

    @Autowired
    private ToolInspectionManagerService service;

    /**
     * 获取待检验的工具
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取待检验的工具数据")
    @GetMapping("/getInspectionTools")
    public Response<PageInfo> getToInspectionList(@CasUser @ApiIgnore EamUser user, ToolQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInspectionTools", Response.class, query);
    }

    /**
     * 获取检验单列表
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取检验单列表")
    @GetMapping("/getInspectionList")
    public Response<PageInfo> getInspectionList(@CasUser @ApiIgnore EamUser user, ToolInspectionQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInspectionList", Response.class, query);
    }

    /**
     * 保存检验单
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "保存检验单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operation", value = "0:保存，1:提交", required = false, dataType = "String", paramType = "query"),
    })
    @PostMapping("/savaInspection")
    public Response<PageInfo> savaInspection(@CasUser @ApiIgnore EamUser user, @RequestBody ToolInspectionBean bean, Integer operation) {
        return ADOConnection.runTask(user.getCurrDs(), service, "savaInspection", Response.class, user, bean, operation);
    }

    /**
     * 获取检验单详情
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "获取检验单详情")
    @GetMapping("/getInspection")
    public Response<ToolInspectionBean> getInspection(@CasUser @ApiIgnore EamUser user,String id) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInspection", Response.class, user, id);
    }

    /**
     * 修改检验单
     *
     * @param user 当前用户
     * @return Resposnse对象
     * @version 1.0
     * @see Response
     */
    @ApiOperation(value = "修改检验单")
    @PostMapping("/updateInspection")
    public Response<PageInfo> updateInspection(@CasUser @ApiIgnore EamUser user, @RequestBody ToolInspectionBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "updateInspection", Response.class, user, bean);
    }

    /**
     * 流程审批回调方法
     *
     * @param redirectQuery
     * @return
     */
    @Override
    public Response updateState(@RequestBody RedirectQuery redirectQuery) {
        // 更新业务状态
        return ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), service, "updateState", Response.class, redirectQuery);
    }


    @SneakyThrows
    @ApiOperation(value = "检验单导出pdf")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检验记录id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/exportPdf")
    public void exportPdf(@CasUser @ApiIgnore EamUser user, HttpServletResponse response, String id){
        try (OutputStream os = response.getOutputStream()) {
            if (null == user) {
                return ;
            }

            response.setContentType("application/pdf");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("检验单.pdf", "UTF-8"));
            ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), service, "exportPdf", void.class,user, id,os);
            os.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @SneakyThrows
    @ApiOperation(value = "导出工器具检验合格证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "检验记录id", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping(value = "/exportInspectionQualification")
    public void exportInspectionQualification(@CasUser @ApiIgnore EamUser user, HttpServletResponse response, String id){
        try (OutputStream os = response.getOutputStream()) {
            if (null == user) {
                return ;
            }

            response.setContentType("application/pdf");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("检验合格证.pdf", "UTF-8"));
            ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), service, "exportInspectionQualification", void.class,user, id,os);
            os.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
